package com.yhg.qxmall.ware.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yhg.qxmall.ware.vo.SkuHasStockVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.yhg.qxmall.ware.entity.WareSkuEntity;
import com.yhg.qxmall.ware.service.WareSkuService;
import com.yhg.common.utils.PageUtils;
import com.yhg.common.utils.R;



/**
 * 商品库存
 *
 * @author 凌月青缃
 * @email linyueqingxiang@gmail.com
 * @date 2020-07-21 23:34:37
 */
@RestController
@RequestMapping("ware/waresku")
public class WareSkuController {
    @Autowired
    private WareSkuService wareSkuService;

    /**************** ============   MQ版防止冗余用 、\/ ==============   ************************/
    @GetMapping("/deleteStock")
    public R deleteStock(@RequestParam Long skuId,@RequestParam Integer count){
        WareSkuEntity skuEntity = wareSkuService.getOne(new QueryWrapper<WareSkuEntity>().eq("sku_id", skuId));
        skuEntity.setStock(skuEntity.getStock()-count);
        wareSkuService.updateById(skuEntity);
//        wareSkuService.deleteStock(skuId);
        return R.ok();
    }

    @GetMapping("getStock")
    public WareSkuEntity getStock(@RequestParam Long skuId){
        WareSkuEntity count = wareSkuService.getOne(new QueryWrapper<WareSkuEntity>().select("stock").eq("sku_id", skuId));
        return count;
    }

    @GetMapping("addStock")
    public Boolean addtoStock(@RequestParam Long skuId,@RequestParam Integer count){
        WareSkuEntity ware = wareSkuService.getOne(new QueryWrapper<WareSkuEntity>().eq("sku_id", skuId));
        if (ware!=null){
            ware.setStock(ware.getStock()+count);
            wareSkuService.updateById(ware);
            return true;
        }else {
            return false;
        }
    }

    /**************** ============   MQ版防止冗余用 /\ ==============   ************************/


    //查询sku是否有库存
    @PostMapping("hasstock")
    public R getSkuHasStock(@RequestBody List<Long> skuIds ){
        //sku_id stock
        List<SkuHasStockVo> vos = wareSkuService.getSkuHasStock(skuIds);
        return R.ok().setData(vos);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = wareSkuService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		WareSkuEntity wareSku = wareSkuService.getById(id);

        return R.ok().put("wareSku", wareSku);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody WareSkuEntity wareSku){
		wareSkuService.save(wareSku);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody WareSkuEntity wareSku){
		wareSkuService.updateById(wareSku);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		wareSkuService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
